define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'virtualmachinepcard/index' + location.search,
                    add_url: 'virtualmachinepcard/add',
                    edit_url: 'virtualmachinepcard/edit',
                    del_url: 'virtualmachinepcard/del',
                    multi_url: 'virtualmachinepcard/multi',
                    import_url: 'virtualmachinepcard/import',
                    table: 'virtualmachine_pcard',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        // {field: 'safety_code', title: __('Safety_code'), operate: 'LIKE'},
                        // {field: 'admin_id_operate', title: __('Admin_id_operate')},
                        {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'emailpassword', title: __('Emailpassword'), operate: 'LIKE'},
                        {field: 'iscustom', title: __('是否绑定账户'), searchList: {"1":__('绑定'),"0":__('暂无')}, formatter: Table.api.formatter.status,custom:{'1': 'success','0':'warning'}},
                        {field: 'usetime', title: __('Usetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('使用中'),"hidden":__('关闭'),"notused":__('未使用'),"confirmclose":__('确认关闭')}, formatter: Table.api.formatter.status,custom:{'normal': 'success','notused':'warning','hidden':'danger','confirmclose':'gray'}},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('操作记录'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('操作记录'),
                                    title: __('操作记录'),
                                    classname: 'btn btn-sm btn-success btn-dialog',
                                    icon: 'fa fa-magic',
                                    url: 'virtualmachinepcardlog/index'
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-virtualmachine").data("formatItem",function (item) {
                return "账户编号:"+item.number+","+item.numbername_email;
            });
            // 监听下拉列表改变的事件
            $("#c-iscustom").bind("change",function () {
                if (this.value== 1){
                    $('#shopifyshow').show();
                }else{
                    $('#shopifyshow').hide();
                }
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-virtualmachine").data("formatItem",function (item) {
                return "账户编号:"+item.number+","+item.numbername_email;
            });
            var iscustom = $("#c-iscustom").find("option:selected").val();
            shopifyshow(iscustom);
            // 监听下拉列表改变的事件
            $("#c-iscustom").bind("change",function () {
                shopifyshow(this.value);
            });
            function shopifyshow(value) {
                if (value== 1){
                    $('#shopifyshow').show();
                }else{
                    $('#shopifyshow').hide();
                }
            }
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});